package gam.t1.appraisal2.dinv;

import gam.t1.appraisal2.DIConfig;
import gam.t1.appraisal2.Data;
import gam.t1.appraisal2.dinv.impl.AppraisalDataInventory;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.util.List;
import java.util.Set;

public class AppraisalConfig extends DIConfig {
    public AppraisalConfig(Plugin plugin, String filePath) {
        super(plugin, filePath);
    }
    @Override
    protected void load() {
        File var1 = new File(plugin.getDataFolder(),filePath);
        if (!var1.exists()){
            var1.getParentFile().mkdirs();
            plugin.saveResource(filePath, false);
        }
        Data.APPRAISAL_DATA_INVENTORIES.clear();
        YamlConfiguration var2 = YamlConfiguration.loadConfiguration(var1);
        Set<String> var3 = var2.getKeys(false);
        for (String var4 : var3) {
            String var2String = var2.getString(var4 + ".name");
            List<String> var2StringList = var2.getStringList(var4 + ".actions");
            List<String> var2StringList2 = var2.getStringList(var4 + ".appraisal");
            Data.APPRAISAL_DATA_INVENTORIES.put(var4,new AppraisalDataInventory(var2String,var2StringList,var2StringList2));
        }
    }

    @Override
    protected void reload() {
        super.reload();
        Bukkit.getOnlinePlayers().forEach($p -> {
            if (Data.getInventory($p.getUniqueId()) != null && Data.getAppraisalDataInventory($p.getUniqueId()) != null ){
                Data.removeInventory($p.getUniqueId());
                Data.removeAppraisalDataInventory($p.getUniqueId());
                $p.closeInventory();
            }
        });
    }
}
